﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Course;
using Model.User;
using System.Web.UI.HtmlControls;
using Model.Communication;

public partial class ViewForum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CourseId"] == null) return;

        Course c = Course.get(Convert.ToInt32(Session["CourseId"]));

        if (c != null)
        {
            courseNameLabel.Text = c.getName();
            courseNameLabel.Font.Size = FontUnit.Parse("1.5em");
            lecturerLabel.Text = "Prowadzący: " + c.getLecturer().getFirstName() + " " + c.getLecturer().getLastName();

            Topic[] topics = Topic.getAll(Forum.get(c));

            foreach (Topic t in topics)
            {
                LinkButton topicButton = new LinkButton();

                topicButton.Text = t.getSubject();
                topicButton.CommandArgument = t.getId().ToString();
                topicButton.Command += topicButton_Command;

                Person author = Person.get(t.getAuthorId());

                Label topicLabel = new Label();
                topicLabel.Text = author.getFirstName() + " " + author.getLastName();

                Label topicLabel2 = new Label();
                topicLabel2.Text = t.getCreated().ToString();

                TableCell cell = new TableCell();
                cell.Controls.Add(topicButton);
                cell.Width = Unit.Percentage(80);

                TableCell cell2 = new TableCell();
                //cell2.Controls.Add(topicLabel);
                //cell2.Controls.Add(new HtmlGenericControl("br"));
                //cell2.Controls.Add(topicLabel2);
                cell2.Text = author.getFirstName() + " " + author.getLastName() + "\n" + t.getCreated().ToString();

                TableRow row = new TableRow();
                row.Cells.Add(cell);
                row.Cells.Add(cell2);

                topicsTable.Rows.Add(row);
            }
        }
    }

    void topicButton_Command(object sender, CommandEventArgs e)
    {
        Session["TopicId"] = e.CommandArgument;
        Session["ActiveMenuButton"] = "menuButton6";
        Response.Redirect("~/CourseView/ViewTopic.aspx");
    }

    protected void toolbarButtonAddTopic_Click(object sender, ImageClickEventArgs e)
    {
        Session["ActiveMenuButton"] = "menuButton6";
        Response.Redirect("~/CourseView/CreateTopicView.aspx");
    }
}